package handler.bid

import service.UserService
import util.Utils

import com.alibaba.fastjson.JSON

/*
公司用户打开二维码显示、打卡统计、修改密码
*/
// 首页
handler.get('/ibid/index'){req, resp ->
	String openid = req.params.openid
	String companyIdStr = req.params.companyId
	// 加过密的
	String pwd = req.params.pwd

	log.info('ibid index ' + openid + ' - ' + companyIdStr + ' - ' + pwd)

	// 默认就登录了
	if(pwd){
		Map user
		if(openid){
			user = UserService.loginByOpenid(openid, pwd)
		}else if(companyIdStr && Utils.isIdValid(companyIdStr)){
			int companyId = companyIdStr as int
			user = UserService.login(companyId, pwd, false)
		}

		if(user){
			req.session('user', user)
			resp.addCookie([name: 'ibid-user-login', value: JSON.toJSONString(user)])
		}else{
			resp.addCookie([name: 'ibid-user-login', value: ''])
		}
	}else{
		resp.addCookie([name: 'ibid-user-login', value: ''])
	}


	def r = [:]
	r.webDebug = 'true' == config.webDebug
	r.dateRangeList = Utils.getRecentWeekList(10)

	resp.render('/ibid/index.html', r)
}

handler.get('/ibid/logout'){req, resp ->
	req.clearSession()
	resp.end ''
}

handler.post('/ibid/change-pwd'){req, resp ->
	req.jsonHandler{json ->
		String pwd = json.pwd

		if(!pwd){
			resp.json([error: 'parameter-invalid'])
			return
		}

		def user = req.session('user')
		if(!user){
			resp.json([error: 'session-invalid'])
			return
		}

		// TODO 4 test
		if(67 == user.companyId){
			resp.json([error: 'this is a test user, change password is not allowed.'])
			return	
		}

		UserService.changePwd(user.companyId, pwd)
		
		resp.end ''
	}
}

handler.post('/ibid/login'){req, resp ->
	req.jsonHandler{json ->
		String companyIdStr = json.companyId
		String pwd = json.pwd
		String isRaw = json.isRaw

		if(!companyIdStr || !pwd || !Utils.isIdValid(companyIdStr)){
			resp.json([error: 'parameter-invalid'])
			return
		}
		int companyId = companyIdStr as int

		def user = UserService.login(companyId, pwd, 'true' == isRaw)
		if(!user){
			user = [flag: false]
		}else{
			user.flag = true
			user.companyId = companyId
			req.session('user', user)
		}

		resp.json(user)
	}
}

// 显示所有的打卡器链接的sockjs client
handler.get('/ibid/sockjs-list'){req, resp ->
	resp.end handlerSockjs.connections*.writeHandlerID.join(',')
}

// 留言
handler.get('/ibid/leave-msg-list'){req, resp ->
	String pwd = req.params.pwd
	// 你能猜到？
	if(!pwd || pwd != 'a0ccbb59315d55831ff1b130b5d989960c854a93'){
		resp.end ''
		return
	}

	String pageNumStr = req.params.pageNum ?: '1'
	if(!Utils.isIdValid(pageNumStr))
		pageNumStr = '1'

	int pageNum = pageNumStr as int
	int pageSize = 10	
	
	def pager = UserService.getLeaveMsgList(pageNum, pageSize)

	resp.render('/ibid/leave-msg-list.html', 
		[pager: pager, hasPre: pager.hasPre(), hasNext: pager.hasNext()])
}

handler.post('/ibid/leave-msg'){req, resp ->
	req.bodyHandler{body -> 
		String msg = new String(body.bytes, 'utf-8')
		UserService.leaveMsg(msg)

		resp.json([flag: true])
	}
}